package com.xsd.rabbitmq.topic;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.xsd.rabbitmq.util.ConnectionUtil;

/**
 * @Description: 生产者
 * @Author: zhoufuzong
 * @Created: 2021-03-14 14:39:35
 * @Version: 1.1.0
 */

public class Producer {
    // 交换机名称
    static final String TOPIC_EXCHANGE="topic_exchange";
    // 队列名称
    static final String TOPIC_QUEUE1="topic_queue1";
    static final String TOPIC_QUEUE2="topic_queue2";

    public static void main(String[] args) throws Exception {
        // 创建连接
        Connection connection = ConnectionUtil.getConnection();
        Channel channel = connection.createChannel();

        // 声明交换机
        channel.exchangeDeclare(TOPIC_EXCHANGE, BuiltinExchangeType.TOPIC);

        String message="rabbitmq topic-routingkey=item.insert";
        channel.basicPublish(TOPIC_EXCHANGE,"item.insert",null,message.getBytes());
        System.out.println("已发送消息="+message);

        message="rabbitmq topic-routingkey=item.update";
        channel.basicPublish(TOPIC_EXCHANGE,"item.update",null,message.getBytes());
        System.out.println("已发送消息="+message);

        message="rabbitmq topic-routingkey=item.delete";
        channel.basicPublish(TOPIC_EXCHANGE,"product.delete",null,message.getBytes());
        System.out.println("已发送消息="+message);

        message="rabbitmq topic other message coming";
        channel.basicPublish(TOPIC_EXCHANGE,"item.delete.abc",null,message.getBytes());
        System.out.println("已发送消息="+message);

        ConnectionUtil.closeConnection(channel,connection);

    }
}
